package com.vunguyen.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vunguyen.model.Bomon;
import com.vunguyen.model.Canbo;
import com.vunguyen.model.Congvan;
import com.vunguyen.model.Congvanden;
import com.vunguyen.model.Giangvien;
import com.vunguyen.model.Phancongcongvanden;
import com.vunguyen.model.Phieubaobu;
import com.vunguyen.model.Phieubaonghi;
import com.vunguyen.model.Phieudangky;
import com.vunguyen.model.Phieudoigiangvien;
import com.vunguyen.model.Phieudoiphong;
import com.vunguyen.service.CongVanService;
import com.vunguyen.service.PhieuDangKyService;

@Controller
public class PhieuDangKyController {
	@Autowired
	private PhieuDangKyService phieuDangKyService;
	
	@Autowired
	private CongVanService congvanService;
	
	@RequestMapping(value = "baonghi", method = RequestMethod.GET)
	public String getListPhieuDangKy(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			m.addAttribute("listPhieuBaoNghi", new Phieudangky());
			m.addAttribute("listPhieuBaoNghi", this.phieuDangKyService.getListPhieubaonghi());
			return "baonghi";
		}
		else{
			return "chuadangnhap";
		}

	}
	@RequestMapping(value = "doigiangvien", method = RequestMethod.GET)
	public String getListPhieuDoiGiangVien(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			int idcanbo =  (int) request.getSession().getAttribute("username");
			List<Giangvien> listGiangvien = congvanService.getGiangvien(idcanbo);
			m.addAttribute("listGiangvien", listGiangvien);
			m.addAttribute("listPhieuDoiGiangVien", new Phieudoigiangvien());
			m.addAttribute("listPhieuDoiGiangVien", this.phieuDangKyService.getListPhieudoigiangvien());
			return "doigiangvien";
		}
		else{
			return "chuadangnhap";
		}

	}
	@RequestMapping(value = "doiphong", method = RequestMethod.GET)
	public String getListPhieuDoiPhong(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			m.addAttribute("listPhieuDoiPhong", new Phieudoiphong());
			m.addAttribute("listPhieuDoiPhong", this.phieuDangKyService.getListPhieudoiphong());
			return "doiphong";
		}
		else{
			return "chuadangnhap";
		}

	}
	@RequestMapping(value = "baobu", method = RequestMethod.GET)
	public String getListPhieuBaoBu(Model m, HttpServletRequest request)
	{
		if(request.getSession().getAttribute("username") != null){
			m.addAttribute("listPhieuBaobu", new Phieubaobu());
			m.addAttribute("listPhieuBaobu", this.phieuDangKyService.getListPhieubaobu());
			return "baobu";
		}
		else{
			return "chuadangnhap";
		}

	}
	//Them phieu bao nghi
    @RequestMapping(value = "/thembaonghi", method = RequestMethod.POST)
    public String themBaoNghi(HttpServletRequest request, Model m) throws ParseException
    {
    	Phieudangky phieudangky = new Phieudangky();
    	Phieubaonghi baonghi = new Phieubaonghi();
    	Giangvien giangvien = new Giangvien();
    	int idcanbo =  (int) request.getSession().getAttribute("username");
    	phieudangky.setTuan(Integer.parseInt(request.getParameter("tuan")));
    	phieudangky.setNgay(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngay")));
    	phieudangky.setGiangvien(giangvien);
    	phieudangky.setTiet(Integer.parseInt(request.getParameter("tiet")));
    	phieudangky.setSoTiet(Integer.parseInt(request.getParameter("sotiet")));
    	phieudangky.setLyDo(request.getParameter("lydo"));
    	phieudangky.setXacNhan(0);
    	giangvien.setIdCanBo(idcanbo);
    	phieudangky.setGiangvien(giangvien);  	
    	phieuDangKyService.themPhieuDangKy(phieudangky);	
    	baonghi.setIdPhieu(phieudangky.getIdPhieu());
    	baonghi.setPhieudangky(phieudangky);
    	baonghi.setMonHoc(request.getParameter("monhoc"));
    	phieuDangKyService.themPhieubaonghi(baonghi);
    	return "redirect:/baonghi";
    }
    // Them phieu bao bu
    @RequestMapping(value = "/thembaobu", method = RequestMethod.POST)
    public String themBaoBu(HttpServletRequest request, Model m) throws ParseException
    {
    	Phieubaobu phieubaobu = new Phieubaobu();
    	Phieubaonghi phieubaonghi = phieuDangKyService.getPhieubaonghiById(Integer.parseInt(request.getParameter("idphieubaonghi")));
    	phieubaobu.setIdPhieuBaoBu(Integer.parseInt(request.getParameter("idphieubaonghi")));
    	phieubaobu.setSiso(Integer.parseInt(request.getParameter("siso")));
    	phieubaobu.setNgayBu(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngaybu")));
    	phieubaobu.setXacNhan(0);
    	phieubaobu.setPhieubaonghi(phieubaonghi);
    	phieuDangKyService.themPhieubaobu(phieubaobu);
    	return "redirect:/baobu";
    }
    //Them phieu doi giang vien
    @RequestMapping(value = "/themdoigiangvien", method = RequestMethod.POST)
    public String themDoiGiangVien(HttpServletRequest request, Model m) throws ParseException
    {
    	Phieudangky phieudangky = new Phieudangky();
    	Phieudoigiangvien doigiangvien = new Phieudoigiangvien();
    	Giangvien giangvien = new Giangvien();
    	int idcanbo =  (int) request.getSession().getAttribute("username");
    	phieudangky.setTuan(Integer.parseInt(request.getParameter("tuan")));
    	phieudangky.setNgay(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngay")));
    	phieudangky.setGiangvien(giangvien);
    	phieudangky.setTiet(Integer.parseInt(request.getParameter("tiet")));
    	phieudangky.setSoTiet(Integer.parseInt(request.getParameter("sotiet")));
    	phieudangky.setLyDo(request.getParameter("lydo"));
    	phieudangky.setXacNhan(0);
    	giangvien.setIdCanBo(idcanbo);
    	phieudangky.setGiangvien(giangvien);  	
    	phieuDangKyService.themPhieuDangKy(phieudangky);
    	doigiangvien.setIdPhieu(phieudangky.getIdPhieu());
    	doigiangvien.setPhieudangky(phieudangky);
    	doigiangvien.setGiangVienDoi(request.getParameter("giangviendoi"));
    	phieuDangKyService.themPhieudoigiangvien(doigiangvien);
    	return "redirect:/doigiangvien";
    }
    //Them phieu doi phong
    @RequestMapping(value = "/themdoiphong", method = RequestMethod.POST)
    public String themDoiPhong(HttpServletRequest request, Model m) throws ParseException
    {
    	Phieudangky phieudangky = new Phieudangky();
    	Phieudoiphong doiphong = new Phieudoiphong();
    	Giangvien giangvien = new Giangvien();
    	int idcanbo =  (int) request.getSession().getAttribute("username");
    	phieudangky.setTuan(Integer.parseInt(request.getParameter("tuan")));
    	phieudangky.setNgay(new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("ngay")));
    	phieudangky.setGiangvien(giangvien);
    	phieudangky.setTiet(Integer.parseInt(request.getParameter("tiet")));
    	phieudangky.setSoTiet(Integer.parseInt(request.getParameter("sotiet")));
    	phieudangky.setLyDo(request.getParameter("lydo"));
    	phieudangky.setXacNhan(0);
    	giangvien.setIdCanBo(idcanbo);
    	phieudangky.setGiangvien(giangvien);  	
    	phieuDangKyService.themPhieuDangKy(phieudangky);
    	doiphong.setIdPhieu(phieudangky.getIdPhieu());
    	doiphong.setPhieudangky(phieudangky);
    	doiphong.setPhongDoi(request.getParameter("phongdoi"));
    	phieuDangKyService.themPhieudoiphong(doiphong);
    	return "redirect:/doiphong";
    }
    //Xoa phieu bao nghi
    @RequestMapping(value = "/xoabaonghi/{idphieu}", method = RequestMethod.GET)
	public String xoaBaoNghi(Model m, @PathVariable("idphieu") int idphieu, HttpServletRequest request)
	{
    	phieuDangKyService.xoaPhieudangky(idphieu);
		return "redirect:/baonghi";
	}
    //Xoa phieu doi giang vien
    @RequestMapping(value = "/xoadoigiangvien/{idphieu}", method = RequestMethod.GET)
	public String xoaDoiGiangVien(Model m, @PathVariable("idphieu") int idphieu, HttpServletRequest request)
	{
    	phieuDangKyService.xoaPhieudangky(idphieu);
		return "redirect:/doigiangvien";
	}
    //Xoa phieu doi phong
    @RequestMapping(value = "/xoadoiphong/{idphieu}", method = RequestMethod.GET)
	public String xoaDoiPhong(Model m, @PathVariable("idphieu") int idphieu, HttpServletRequest request)
	{
    	phieuDangKyService.xoaPhieudangky(idphieu);
		return "redirect:/doiphong";
	}

	


}
